package com.andatsoft.laisim.activity;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.asynctask.ConfigSaverTask;
import com.andatsoft.laisim.audioeffect.AudioEffect;
import com.andatsoft.laisim.audioeffect.AudioEffect.ApplyMode;
import com.andatsoft.laisim.audioeffect.AudioEffectManager;
import com.andatsoft.laisim.config.LaisimConfig;
import com.andatsoft.laisim.db.DataBaseAccess;
import com.andatsoft.laisim.dialog.PresetDialog;
import com.andatsoft.laisim.service.PlayerService;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.ToastMessage;
import com.andatsoft.laisim.utils.Utils;
import com.andatsoft.laisim.view.CircularProgressBar;
import com.andatsoft.laisim.view.CircularProgressBar.IProgressChanged;
import com.andatsoft.laisim.view.SlimVerticalProgressBar;
import com.andatsoft.laisim.view.SlimVerticalProgressBar.SlimProgressClicked;
import com.andatsoft.laisim.view.Toolbox;

public class SoundEffectActivity extends BaseActivity {

	public static final int REQUEST_CODE_PRESET_DIALOG = 299;

	/*
	 * Anym_effect: id = -5;
	 */
	// private VerticalSeekBar mSeekBar60;
	private SlimVerticalProgressBar mSeekBar60;
	private SlimVerticalProgressBar mSeekBar230;
	private SlimVerticalProgressBar mSeekBar910;
	private SlimVerticalProgressBar mSeekBar3k6;
	private SlimVerticalProgressBar mSeekBar14k;

	private CircularProgressBar mSeekBarBassBoost;
	private CircularProgressBar mSeekBarVolumn;
	private CircularProgressBar mSeekBarBalance;

	private Button mBtnEqOnOff;
	private Button mBtnBassOnOff;
	private Button mBtnReset;
	private Button mBtnApplyTo;

	private ToastMessage mToast;
	private Toolbox mToolbox;

	private TextView mTvPreset;

	// eq config
	private int mMinBandValue;

	// vars
	private float mleftVolume;
	private float mRightVolume;
	private AudioEffect mAudioEffect;

	private ApplyMode mApplyMode = ApplyMode.CURRENT_SONG;

	// Player service
	private PlayerService mPlayerService;
	private boolean mIsBoundService;

	private ServiceConnection mPlayerServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mPlayerService = null;
		}

		// called when bind service called.
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mPlayerService = ((PlayerService.LocalBinder) service).getService();
			mIsBoundService = true;
			// initAudioEffect();
			initEqualizer();
			initBassBoost();
			initVolumn();
			mApplyMode = mAudioEffect.getApplyMode();
			updateViews();
		}
	};

	public void doBindService() {
		bindService(new Intent(SoundEffectActivity.this, PlayerService.class),
				mPlayerServiceConnection, Context.BIND_AUTO_CREATE);
		// Log.d("Bind", "bind" + bo);
		mIsBoundService = true;
	}

	public void doUnBindService() {
		unbindService(mPlayerServiceConnection);
		mIsBoundService = false;
	}

	private BroadcastReceiver mBroadcastUpdateSongInfo = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (mPlayerService == null)
				return;
			mAudioEffect = AudioEffectManager.getCurrentAudioEffect();
			updateViews();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ThemeManager.loadSkin(getApplicationContext(),
				ThemeManager.SOUND_EFFECT_ACTIVYTY);
		setContentView(R.layout.activity_sound_effect);
		mToast = new ToastMessage(getApplicationContext());
		doBindService();
		mAudioEffect = AudioEffectManager.getCurrentAudioEffect();
		// applyGeneralEqSetting();
		initViews();
		setupListeners();
	}

	// private void initAudioEffect() {
	// // init audio effect
	// AudioEffectManager.initAudioEffect(mPlayerService.getAudioSessionId());
	// int songEff = mPlayerService.getCurrentSongEffectId();
	// AudioEffectManager.loadEffect(getApplicationContext(), songEff);
	// mAudioEffect = AudioEffectManager.getCurrentAudioEffect();
	//
	// }

	private void applyTheme() {
		((TextView) findViewById(R.id.tv_eq_60)).setTextColor(ThemeManager
				.getSelectedTheme().getNormalTextColor());
		((TextView) findViewById(R.id.tv_eq_230)).setTextColor(ThemeManager
				.getSelectedTheme().getNormalTextColor());
		((TextView) findViewById(R.id.tv_eq_910)).setTextColor(ThemeManager
				.getSelectedTheme().getNormalTextColor());
		((TextView) findViewById(R.id.tv_eq_3k6)).setTextColor(ThemeManager
				.getSelectedTheme().getNormalTextColor());
		((TextView) findViewById(R.id.tv_eq_14k)).setTextColor(ThemeManager
				.getSelectedTheme().getNormalTextColor());
		((TextView) findViewById(R.id.tv_static_effect_act_bal_l))
				.setTextColor(ThemeManager.getSelectedTheme()
						.getNormalTextColor());
		((TextView) findViewById(R.id.tv_static_effect_act_bal_r))
				.setTextColor(ThemeManager.getSelectedTheme()
						.getNormalTextColor());
		((TextView) findViewById(R.id.tv_static_effect_act_balance))
				.setTextColor(ThemeManager.getSelectedTheme()
						.getNormalTextColor());
		((TextView) findViewById(R.id.tv_static_effect_act_vol))
				.setTextColor(ThemeManager.getSelectedTheme()
						.getNormalTextColor());
		((TextView) findViewById(R.id.tv_static_effect_act_bass))
				.setTextColor(ThemeManager.getSelectedTheme()
						.getNormalTextColor());

	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	private void applySkin() {
		if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD_TO_ICS) {
			((LinearLayout) findViewById(R.id.layout_effect_eq_background))
					.setBackground(ThemeManager.getSelectedSkin()
							.getEffectActivityAboveBackground());
			((LinearLayout) findViewById(R.id.layout_effect_balance_background))
					.setBackground(ThemeManager.getSelectedSkin()
							.getEffectActivityBelowBackground());
		} else {
			((LinearLayout) findViewById(R.id.layout_effect_eq_background))
					.setBackgroundDrawable(ThemeManager.getSelectedSkin()
							.getEffectActivityAboveBackground());
			((LinearLayout) findViewById(R.id.layout_effect_balance_background))
					.setBackgroundDrawable(ThemeManager.getSelectedSkin()
							.getEffectActivityBelowBackground());
		}
	}

	@SuppressLint("NewApi")
	private void updateViews() {
		if (mAudioEffect == null)
			return;
		// eq
		try {
			mSeekBar60.setProgress(mAudioEffect.getEq60());
			AudioEffectManager.equalizer.setBandLevel((short) 0,
					(short) mAudioEffect.getEq60());
			mSeekBar230.setProgress(mAudioEffect.getEq230());
			AudioEffectManager.equalizer.setBandLevel((short) 1,
					(short) mAudioEffect.getEq230());
			mSeekBar910.setProgress(mAudioEffect.getEq910());
			AudioEffectManager.equalizer.setBandLevel((short) 2,
					(short) mAudioEffect.getEq910());
			mSeekBar3k6.setProgress(mAudioEffect.getEq3k6());
			AudioEffectManager.equalizer.setBandLevel((short) 3,
					(short) mAudioEffect.getEq3k6());
			mSeekBar14k.setProgress(mAudioEffect.getEq14k());
			AudioEffectManager.equalizer.setBandLevel((short) 4,
					(short) mAudioEffect.getEq14k());

			if (LaisimConfig.isEqEnable) {
				if (mAudioEffect.isEqEnable()) {
					setDrawableForView(mBtnEqOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectEqOnOn());
					AudioEffectManager.equalizer.setEnabled(true);
				} else {
					setDrawableForView(mBtnEqOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectEqOnOff());
					AudioEffectManager.equalizer.setEnabled(false);
				}
			} else {
				if (mAudioEffect.isEqEnable()) {
					setDrawableForView(mBtnEqOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectEqOffOn());
					AudioEffectManager.equalizer.setEnabled(false);
				} else {
					setDrawableForView(mBtnEqOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectEqOffOff());
					AudioEffectManager.equalizer.setEnabled(false);
				}
			}

			if (LaisimConfig.isBassEnable) {
				if (mAudioEffect.isBassboostEnable()) {
					setDrawableForView(mBtnBassOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectBassOnOn());
					AudioEffectManager.bassBoost.setEnabled(true);
				} else {
					setDrawableForView(mBtnBassOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectBassOnOff());
					AudioEffectManager.bassBoost.setEnabled(false);
				}
			} else {
				if (mAudioEffect.isBassboostEnable()) {
					setDrawableForView(mBtnBassOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectBassOffOn());
					AudioEffectManager.bassBoost.setEnabled(false);
				} else {
					setDrawableForView(mBtnBassOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectBassOffOff());
					AudioEffectManager.bassBoost.setEnabled(false);
				}
			}

			// bass
			mSeekBarBassBoost.setProgress(mAudioEffect.getBassboostStrength());
			AudioEffectManager.bassBoost.setStrength((short) mAudioEffect
					.getBassboostStrength());
			// vol and balance
			mSeekBarVolumn.setProgress(mAudioEffect.getVolume());
			if (!mIsBoundService)
				return;
			float vol = (float) mAudioEffect.getVolume() / 1000;
			mPlayerService.setVolume(vol, vol);
			mSeekBarBalance.setProgress(mAudioEffect.getBalance());
			// balance
			int progress = mAudioEffect.getBalance();
			if (progress <= 50) {
				// balance to left, so we need to reduce volume on
				// right
				mleftVolume = 1.0f;
				mRightVolume = (float) (progress * 2) / 100;

			} else {
				// right
				// balance to right, so we need to reduce volume on
				// left
				mRightVolume = 1.0f;
				mleftVolume = (float) ((100 - progress) * 2) / 100;
			}
			mPlayerService.setVolume(mleftVolume, mRightVolume);

			switch (mAudioEffect.getApplyMode()) {
			case ALL_SONG:
				mBtnApplyTo.setText("AM: A");
				setDrawableForView(mBtnApplyTo, ThemeManager.getSelectedSkin()
						.getBtnEffectAMA());
				break;
			case CURRENT_GROUP:
				setDrawableForView(mBtnApplyTo, ThemeManager.getSelectedSkin()
						.getBtnEffectAMG());
				break;
			case CURRENT_SONG:
				setDrawableForView(mBtnApplyTo, ThemeManager.getSelectedSkin()
						.getBtnEffectAMS());
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void initViews() {
		mToolbox = (Toolbox) findViewById(R.id.layout_toolbox_sound_effect);
		mSeekBar60 = (SlimVerticalProgressBar) findViewById(R.id.seekbar_60);
		mSeekBar230 = (SlimVerticalProgressBar) findViewById(R.id.seekbar_230);
		mSeekBar910 = (SlimVerticalProgressBar) findViewById(R.id.seekbar_910);
		mSeekBar3k6 = (SlimVerticalProgressBar) findViewById(R.id.seekbar_3k6);
		mSeekBar14k = (SlimVerticalProgressBar) findViewById(R.id.seekbar_14k);
		//
		mSeekBarBassBoost = (CircularProgressBar) findViewById(R.id.seekbar_bassboost);
		mSeekBarVolumn = (CircularProgressBar) findViewById(R.id.seekbar_volumn);
		mSeekBarBalance = (CircularProgressBar) findViewById(R.id.seekbar_balance);

		//
		mBtnEqOnOff = (Button) findViewById(R.id.btn_sound_effect_eq_on_off);
		mBtnBassOnOff = (Button) findViewById(R.id.btn_sound_effect_bass_on_off);
		mBtnReset = (Button) findViewById(R.id.btn_sound_effect_reset);
		setDrawableForView(mBtnReset, ThemeManager.getSelectedSkin()
				.getBtnEffectReset());
		mBtnApplyTo = (Button) findViewById(R.id.btn_sound_effect_apply_mode);

		//
		mTvPreset = (TextView) findViewById(R.id.tv_eq_name);
		mTvPreset.setTextColor(ThemeManager.getSelectedTheme()
				.getNormalTextColor());

	}

	private boolean mIsLongTouchEq;
	private boolean mIsLongTouchBass;

	private void setupListeners() {

		mBtnApplyTo.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					showToolbox();
					return true;
				}
				return true;

			}
		});

		mTvPreset.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					Intent in = new Intent(SoundEffectActivity.this,
							PresetDialog.class);
					startActivityForResult(in, REQUEST_CODE_PRESET_DIALOG);
					return true;
				}
				return false;
			}
		});

		mBtnEqOnOff.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return false;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					if (mIsLongTouchEq) {
						mIsLongTouchEq = false;
						return false;
					}
					if (LaisimConfig.isEqEnable) {
						if (mAudioEffect.isEqEnable()) {
							mAudioEffect.setEqEnable(false);
							AudioEffectManager.equalizer.setEnabled(false);
							mToast.showLongMessage(getString(R.string.main_eq_off));
							setDrawableForView(mBtnEqOnOff, ThemeManager
									.getSelectedSkin().getBtnEffectEqOnOff());
						} else {
							try {
								// affect
								AudioEffectManager.equalizer.setBandLevel(
										(short) 0,
										(short) mSeekBar60.getProgress());
								AudioEffectManager.equalizer.setBandLevel(
										(short) 1,
										(short) mSeekBar230.getProgress());
								AudioEffectManager.equalizer.setBandLevel(
										(short) 2,
										(short) mSeekBar910.getProgress());
								AudioEffectManager.equalizer.setBandLevel(
										(short) 3,
										(short) mSeekBar3k6.getProgress());
								AudioEffectManager.equalizer.setBandLevel(
										(short) 4,
										(short) mSeekBar14k.getProgress());
								mAudioEffect.setEqEnable(true);
								AudioEffectManager.equalizer.setEnabled(true);
								mToast.showLongMessage(getString(R.string.main_eq_on));
								setDrawableForView(mBtnEqOnOff, ThemeManager
										.getSelectedSkin().getBtnEffectEqOnOn());
							} catch (Exception e) {

							}
						}
					} else {
						if (mAudioEffect.isEqEnable()) {
							mAudioEffect.setEqEnable(false);
							mToast.showLongMessage(getString(R.string.main_eq_off));
							setDrawableForView(mBtnEqOnOff, ThemeManager
									.getSelectedSkin().getBtnEffectEqOffOff());
						} else {
							mAudioEffect.setEqEnable(true);
							mToast.showLongMessage(getString(R.string.eq_toast_eq_turn_on_but));
							setDrawableForView(mBtnEqOnOff, ThemeManager
									.getSelectedSkin().getBtnEffectEqOffOn());
						}
					}
					return false;
				}
				return false;
			}
		});

		mBtnEqOnOff.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				try {
					AudioEffectManager.equalizer.setEnabled(true);
					mAudioEffect.setEqEnable(true);
					LaisimConfig.isEqEnable = true;
					// affect
					AudioEffectManager.equalizer.setBandLevel((short) 0,
							(short) mSeekBar60.getProgress());
					AudioEffectManager.equalizer.setBandLevel((short) 1,
							(short) mSeekBar230.getProgress());
					AudioEffectManager.equalizer.setBandLevel((short) 2,
							(short) mSeekBar910.getProgress());
					AudioEffectManager.equalizer.setBandLevel((short) 3,
							(short) mSeekBar3k6.getProgress());
					AudioEffectManager.equalizer.setBandLevel((short) 4,
							(short) mSeekBar14k.getProgress());
					mToast.showLongMessage(getString(R.string.main_eq_on));
					setDrawableForView(mBtnEqOnOff, ThemeManager
							.getSelectedSkin().getBtnEffectEqOnOn());
					mIsLongTouchEq = true;
				} catch (Exception e) {
				}
				return true;
			}
		});

		mBtnBassOnOff.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return false;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					if (mIsLongTouchBass) {
						mIsLongTouchBass = false;
						return false;
					}
					if (LaisimConfig.isBassEnable) {
						if (mAudioEffect.isBassboostEnable()) {
							mAudioEffect.setBassboostEnable(false);
							AudioEffectManager.bassBoost.setEnabled(false);
							mToast.showLongMessage(getString(R.string.main_bass_off));
							setDrawableForView(mBtnBassOnOff, ThemeManager
									.getSelectedSkin().getBtnEffectBassOnOff());
						} else {
							mAudioEffect.setBassboostEnable(true);
							AudioEffectManager.bassBoost.setEnabled(true);
							mToast.showLongMessage(getString(R.string.main_bass_on));
							setDrawableForView(mBtnBassOnOff, ThemeManager
									.getSelectedSkin().getBtnEffectBassOnOn());
						}
					} else {
						if (mAudioEffect.isBassboostEnable()) {
							mAudioEffect.setBassboostEnable(false);
							mToast.showLongMessage(getString(R.string.main_bass_off));
							setDrawableForView(mBtnBassOnOff, ThemeManager
									.getSelectedSkin().getBtnEffectBassOffOff());
						} else {
							mAudioEffect.setBassboostEnable(true);
							mToast.showLongMessage(getString(R.string.eq_toast_bass_turn_on_but));
							setDrawableForView(mBtnBassOnOff, ThemeManager
									.getSelectedSkin().getBtnEffectBassOffOn());
						}
					}
					return false;
				}
				return false;
			}
		});

		mBtnBassOnOff.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				AudioEffectManager.bassBoost.setEnabled(true);
				mAudioEffect.setBassboostEnable(true);
				LaisimConfig.isBassEnable = true;
				// bass boost
				AudioEffectManager.bassBoost
						.setStrength((short) mSeekBarBassBoost.getProgress());
				mToast.showLongMessage(getString(R.string.main_bass_on));
				mIsLongTouchBass = true;
				setDrawableForView(mBtnBassOnOff, ThemeManager
						.getSelectedSkin().getBtnEffectBassOnOn());
				return true;
			}
		});

		mBtnReset.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					// reset eq
					mSeekBar60.setProgress(mSeekBar60.getMax() / 2);
					mSeekBar230.setProgress(mSeekBar230.getMax() / 2);
					mSeekBar910.setProgress(mSeekBar910.getMax() / 2);
					mSeekBar3k6.setProgress(mSeekBar3k6.getMax() / 2);
					mSeekBar14k.setProgress(mSeekBar14k.getMax() / 2);

					// reset bass boost to 0
					mSeekBarBassBoost.setProgress(0);
					// reset balance to normal ( 50)
					mSeekBarBalance.setProgress(50);
					try {

						// affect
						AudioEffectManager.equalizer.setBandLevel((short) 0,
								(short) mSeekBar60.getProgress());
						AudioEffectManager.equalizer.setBandLevel((short) 1,
								(short) mSeekBar230.getProgress());
						AudioEffectManager.equalizer.setBandLevel((short) 2,
								(short) mSeekBar910.getProgress());
						AudioEffectManager.equalizer.setBandLevel((short) 3,
								(short) mSeekBar3k6.getProgress());
						AudioEffectManager.equalizer.setBandLevel((short) 4,
								(short) mSeekBar14k.getProgress());
						// bass boost
						AudioEffectManager.bassBoost.setStrength((short) 0);
					} catch (Exception e) {
					}

					// Save info
					mAudioEffect.setEq60(mSeekBar60.getProgress());
					mAudioEffect.setEq230(mSeekBar230.getProgress());
					mAudioEffect.setEq910(mSeekBar910.getProgress());
					mAudioEffect.setEq3k6(mSeekBar3k6.getProgress());
					mAudioEffect.setEq14k(mSeekBar14k.getProgress());

					mAudioEffect.setBalance(mSeekBarBalance.getProgress());
					mAudioEffect.setBassboostStrength(mSeekBarBassBoost
							.getProgress());
					return true;
				}
				return true;
			}
		});

		mSeekBarBassBoost.setOnProgressChanged(new IProgressChanged() {

			@Override
			public void onProgressChanged(int newProgress) {
				AudioEffectManager.bassBoost.setStrength((short) newProgress);
				mAudioEffect.setBassboostStrength(newProgress);
			}
		});

		mSeekBarVolumn.setOnProgressChanged(new IProgressChanged() {

			@Override
			public void onProgressChanged(int newProgress) {
				if (!mIsBoundService)
					return;
				float vol = (float) newProgress / 1000;
				mPlayerService.setVolume(vol, vol);
				mAudioEffect.setVolume(newProgress);
			}
		});

		mSeekBarBalance.setOnProgressChanged(new IProgressChanged() {

			@Override
			public void onProgressChanged(int progress) {
				if (progress <= 50) {
					// balance to left, so we need to reduce volume on
					// right
					mleftVolume = 1.0f;
					mRightVolume = (float) (progress * 2) / 100;

				} else {
					// right
					// balance to right, so we need to reduce volume on
					// left
					mRightVolume = 1.0f;
					mleftVolume = (float) ((100 - progress) * 2) / 100;
				}
				mPlayerService.setVolume(mleftVolume, mRightVolume);
				mAudioEffect.setBalance(progress);
			}
		});

		// mSeekBarBalance
		// .setOnSeekBarChangeListener(new OnCircularSeekBarChangeListener() {
		//
		// @Override
		// public void onStopTrackingTouch(CircularSeekBar seekBar) {
		//
		// }
		//
		// @Override
		// public void onStartTrackingTouch(CircularSeekBar seekBar) {
		//
		// }
		//
		// @Override
		// public void onProgressChanged(
		// CircularSeekBar circularSeekBar, int progress,
		// boolean fromUser) {
		// if (progress <= 50) {
		// // balance to left, so we need to reduce volume on
		// // right
		// mleftVolume = 1.0f;
		// mRightVolume = (float) (progress * 2) / 100;
		//
		// } else {
		// // right
		// // balance to right, so we need to reduce volume on
		// // left
		// mRightVolume = 1.0f;
		// mleftVolume = (float) ((100 - progress) * 2) / 100;
		// }
		// mPlayerService.setVolume(mleftVolume, mRightVolume);
		// mAudioEffect.setBalance(progress);
		// }
		// });

		mSeekBar60.setOnSlimClicked(new SlimProgressClicked() {

			@Override
			public void onClick(int newProgress) {
				AudioEffectManager.equalizer.setBandLevel((short) 0,
						(short) (newProgress + mMinBandValue));
				mAudioEffect.setEq60(mSeekBar60.getProgress());
				Log.e("seek60",
						AudioEffectManager.equalizer.getBandLevel((short) 0)
								+ "");
			}
		});

		mSeekBar230.setOnSlimClicked(new SlimProgressClicked() {

			@Override
			public void onClick(int newProgress) {
				AudioEffectManager.equalizer.setBandLevel((short) 1,
						(short) (newProgress + mMinBandValue));
				mAudioEffect.setEq230(mSeekBar230.getProgress());
				Log.e("seek230", mSeekBar230.getProgress() + "," + newProgress);
			}
		});

		mSeekBar910.setOnSlimClicked(new SlimProgressClicked() {

			@Override
			public void onClick(int newProgress) {
				AudioEffectManager.equalizer.setBandLevel((short) 2,
						(short) (newProgress + mMinBandValue));
				mAudioEffect.setEq910(mSeekBar910.getProgress());
			}
		});

		mSeekBar3k6.setOnSlimClicked(new SlimProgressClicked() {

			@Override
			public void onClick(int newProgress) {
				AudioEffectManager.equalizer.setBandLevel((short) 3,
						(short) (newProgress + mMinBandValue));
				mAudioEffect.setEq3k6(mSeekBar3k6.getProgress());
			}
		});

		mSeekBar14k.setOnSlimClicked(new SlimProgressClicked() {

			@Override
			public void onClick(int newProgress) {
				AudioEffectManager.equalizer.setBandLevel((short) 4,
						(short) (newProgress + mMinBandValue));
				mAudioEffect.setEq14k(mSeekBar14k.getProgress());
			}
		});

	}

	private void initEqualizer() {
		if (AudioEffectManager.equalizer == null)
			return;
		mMinBandValue = AudioEffectManager.equalizer.getBandLevelRange()[0];
		final int seekMax = AudioEffectManager.equalizer.getBandLevelRange()[1]
				- mMinBandValue;
		if (LaisimConfig.isEqEnable && mAudioEffect.isEqEnable()) {
			AudioEffectManager.equalizer.setEnabled(true);
		} else {
			AudioEffectManager.equalizer.setEnabled(false);
		}
		mSeekBar60.setMax(seekMax);
		mSeekBar230.setMax(seekMax);
		mSeekBar910.setMax(seekMax);
		mSeekBar3k6.setMax(seekMax);
		mSeekBar14k.setMax(seekMax);
	}

	private void initBassBoost() {
		if (AudioEffectManager.bassBoost == null)
			return;
		mSeekBarBassBoost.setMax(1000);
		if (LaisimConfig.isBassEnable && mAudioEffect.isBassboostEnable()) {
			AudioEffectManager.bassBoost.setEnabled(true);
		} else {
			AudioEffectManager.bassBoost.setEnabled(false);
		}
	}

	private void initVolumn() {
		mSeekBarVolumn.setMax(1000);
	}

	private void closeToolbox() {
		mToolbox.close();
	}

	private void showToolbox() {
		if (mToolbox.isShowing())
			return;
		mToolbox.setContentLayout(R.layout.tool_box_sound_effect);
		mToolbox.getContent().setPadding(10, 0, 10, 0);
		mToolbox.show();
		setBackgroundColorForView(mToolbox.getContent());
		TextView btnAll = (TextView) findViewById(R.id.tv_toolbox_sound_effect_all);

		btnAll.setTextColor(ThemeManager.getSelectedTheme()
				.getNormalTextColor());
		btnAll.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					mApplyMode = ApplyMode.ALL_SONG;
					setDrawableForView(mBtnApplyTo, ThemeManager
							.getSelectedSkin().getBtnEffectAMA());
					mToast.showShortMessage(getString(R.string.eq_toast_ama_turn_on));
					// Create animation
					overridePendingTransition(R.anim.fade_in_fast,
							R.anim.fade_out_fast);
					closeToolbox();
					return true;
				}
				return false;
			}
		});

		TextView btnGroup = (TextView) findViewById(R.id.tv_toolbox_sound_effect_group);

		btnGroup.setTextColor(ThemeManager.getSelectedTheme()
				.getNormalTextColor());
		btnGroup.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					mApplyMode = ApplyMode.CURRENT_GROUP;
					setDrawableForView(mBtnApplyTo, ThemeManager
							.getSelectedSkin().getBtnEffectAMG());
					mToast.showShortMessage(getString(R.string.eq_toast_amg_turn_on));
					overridePendingTransition(R.anim.fade_in_fast,
							R.anim.fade_out_fast);
					closeToolbox();
					return true;
				}
				return false;
			}
		});

		TextView btnSong = (TextView) findViewById(R.id.tv_toolbox_sound_effect_song);

		btnSong.setTextColor(ThemeManager.getSelectedTheme()
				.getNormalTextColor());
		btnSong.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					mApplyMode = ApplyMode.CURRENT_SONG;
					setDrawableForView(mBtnApplyTo, ThemeManager
							.getSelectedSkin().getBtnEffectAMS());
					mToast.showShortMessage(getString(R.string.eq_toast_ams_turn_on));
					overridePendingTransition(R.anim.fade_in_fast,
							R.anim.fade_out_fast);
					closeToolbox();
					return true;
				}
				return false;
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// mEnvironmentalReverb.release();
		doUnBindService();
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(mBroadcastUpdateSongInfo);
		// saveGeneralEqSetting();
	}

	@Override
	protected void onResume() {
		super.onResume();
		applyTheme();
		applySkin();
		registerReceiver(mBroadcastUpdateSongInfo, new IntentFilter(
				Common.INTENT_UPDATE_SONG_INFO));
	}

	//
	// private void saveGeneralEqSetting() {
	// SharedPreferences sp = getSharedPreferences(
	// Common.SP_SOUND_EFFECT_NAME, Context.MODE_PRIVATE);
	// SharedPreferences.Editor ed = sp.edit();
	//
	// ed.putBoolean(Common.SP_SOUND_EFFECT_IS_EQ, AudioEffectManager.isEq);
	// ed.putBoolean(Common.SP_SOUND_EFFECT_IS_BASS,
	// AudioEffectManager.isBassboost);
	//
	// ed.commit();
	// }

	// private void applyGeneralEqSetting() {
	// SharedPreferences sp = getSharedPreferences(
	// Common.SP_SOUND_EFFECT_NAME, Context.MODE_PRIVATE);
	//
	// mIsEq = sp.getBoolean(Common.SP_SOUND_EFFECT_IS_EQ, true);
	// mIsBass = sp.getBoolean(Common.SP_SOUND_EFFECT_IS_BASS, true);
	// }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE_PRESET_DIALOG) {
			if (resultCode == PresetDialog.RESULT_CODE_FOR_SOUND_EFFECT_ACTIVITY) {
				mAudioEffect = (AudioEffect) data
						.getSerializableExtra(PresetDialog.INTENT_PRESET_CHANGED);
				AudioEffectManager.mAudioEffect = mAudioEffect;
				// re update views
				updateViews();

			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private class saveTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			if (mPlayerService.getCurrentSong() == null)
				return null;
			DataBaseAccess db = DataBaseAccess
					.getInstance(getApplicationContext());
			// int effectId = mAudioEffect.getId();
			switch (mApplyMode) {
			case ALL_SONG:
				List<Integer> songIds = mPlayerService.getSongIds();
				// delete all custom effect set before
				db.deleteAllCustomEffect();
				// add new effect for all song
				mAudioEffect.setName("All Song");
				// the 1 flag means we can delete it when necessary
				int newId = db.AddAudioEffect(mAudioEffect, 1);
				db.setAudioEffectForGroup(songIds, newId);
				// update effect id for all song
				mPlayerService.updateEffectIdForSongs(songIds, newId);

				break;
			case CURRENT_GROUP:
				// For current group, we should not delete all effect set
				// before.
				// Just create a new effect one and set it for all song in the
				// group
				List<Integer> songIdsg = mPlayerService.getSongIds();
				// add new effect for all song
				mAudioEffect.setName("Group");
				// the 1 flag means we can delete it when necessary
				int newIdG = db.AddAudioEffect(mAudioEffect, 1);
				db.setAudioEffectForGroup(songIdsg, newIdG);
				// update effect id for all song in the group
				mPlayerService.updateEffectIdForSongs(songIdsg, newIdG);
				break;
			case CURRENT_SONG:
				int songId = mPlayerService.getCurrentSong().getId();
				// add new effect for this song
				mAudioEffect.setName("Song " + songId);
				int newIdS = db.AddAudioEffect(mAudioEffect, 1);
				db.setAudioEffectForSong(songId, newIdS);
				mPlayerService.updateEffectIdForSong(songId, newIdS);

				break;
			}
			return null;
		}

	}

	@Override
	public void onBackPressed() {

		if (mToolbox.isShowing()) {
			closeToolbox();
		} else {
			super.onBackPressed();
			overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);

			// save the effect id to the current song
			new saveTask().execute();
			// save config
			if (AudioEffectManager.equalizer.getEnabled())
				LaisimConfig.isEqEnable = true;
			if (AudioEffectManager.bassBoost.getEnabled())
				LaisimConfig.isBassEnable = true;
			new ConfigSaverTask(getApplicationContext()).execute();
			ThemeManager.getSelectedSkin().release(
					ThemeManager.SOUND_EFFECT_ACTIVYTY);
			unbindDrawables(findViewById(R.id.root_view_sound_effect_activity));
			System.gc();
			super.onBackPressed();
		}
	}

}
